<template>
	<ly-tree 
		:props="props" 
		node-key="id" 
		:load="loadNode" 
		lazy  
		@node-click="handleNodeClick"
	/>
</template>

<script>
	import LyTree from '@/components/ly-tree/ly-tree.vue'
	import { getTopTreeList, getSubTreeList } from '@/api/base/equipment.js'
	
	export default {
		components: {
			LyTree
		},
		props: {
			data: {
				type: Array,
				default() {
					return []
				}
			}
		},
		data() {
			return {
				treeData: this.data,
				props: {
					label: 'name',
					children: 'zones',
					isLeaf: 'leaf'
				}
			};
		},
		methods: {
			loadNode(node, resolve) {
				let data = []
				if (node.level === 0) {
					this.initTopTreeData(node.key).then(res=>{
						data = res
						resolve(data);
					})
				} else {
					this.initSubTreeData(node.key).then(res=>{
						data = res
						resolve(data);
					})
				}
				
				// if (node.level === 0) {
				// 	resolve([{
				// 		id: this.data[0].id,
				// 		name: this.data[0].label
				// 	}]);
				// } else {
				// 	let data = []
				// 	this.initEquipLocationData(node.key).then(res=>{
				// 		data = res
						
				// 		resolve(data);
				// 	})
				// }
			},
			handleNodeClick(data) {
				this.$emit("clickTreeNode", data)
			},
			async initTopTreeData(locaId) {
				let list = []
				// await getTopTreeList(uni.getStorageSync('companyId')).then(res => {
				await getTopTreeList('').then(res => {
					res.data.forEach(item => {
						list.push({
							id: item.key,
							name: item.title,
							pathData: item.data,
							children: item.children
						})
					})
				})
				return list
			},
			async initSubTreeData(locaId) {
				let list = []
				await getSubTreeList(locaId).then(res=>{
					res.data.forEach(item => {
						list.push({
							id: item.key,
							name: item.title,
							pathData: item.data,
						})
					})
				})
				return list
			}
		}
	};
</script>
